/*
* @Author: dengjiayao
* @Date:   2017-06-28 09:28:03
 * @Last Modified by: songwh
 * @Last Modified time: 2018-04-11 14:00:52
*/

import Vue from 'vue'

const SUCCESS = 200
const API_ROOT = server.castTalkRoot
const QINIU_API_ROOT = server.qiniuRoot

const http = (api, params) => {
  let path = api.replace(/\./g, '/')
  let httpResource = Vue.resource(API_ROOT + '/' + path, {}, {}, {
    emulateJSON: false,
    headers: {
      'x-token': 'd6eea01631114d5a9bbc819de4ce2127'
    },
    responseType: 'json'
  })
  return httpResource.save(params)
}

// 投研大咖说列表
export const getCastTalkList = ({dispatch}, params) => {
  return http('circle.CircleServiceImpl.voices', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 0)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

// 创建投研大咖说
export const createCastTalk = ({dispatch}, params) => {
  return http('circle.CircleServiceImpl.publishVoice2', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('CAST_TALK_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

// 创建投研大咖说
export const createCastTalkOld = ({dispatch}, params) => {
  return http('circle.CircleServiceImpl.publishVoice', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('CAST_TALK_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

// 删除投研大咖说
export const deleteCastTalk = ({dispatch}, params) => {
  return http('circle.CircleServiceImpl.delMessage', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('CAST_TALK_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

// 用户反馈列表
export const getCastTalkFeedbackList = ({dispatch}, params) => {
  return http('user.UserServiceImpl.needList', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 0)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

// 用户白名单
export const getCastTalkWhiteList = ({dispatch}, params) => {
  return http('user.UserOpsServiceImpl.queryVoiceUser', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 0)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

// 添加用户
export const addCastTalkUser = ({dispatch}, params) => {
  return http('user.UserOpsServiceImpl.createVoiceUser', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('CAST_TALK_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

// 更新用户信息
export const updateCastTalkUser = ({dispatch}, params) => {
  return http('user.UserOpsServiceImpl.updateVoiceUser', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('CAST_TALK_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

// 非白名单登陆用户列表
export const getCastTalkUserLoginFail = ({dispatch}, params) => {
  return http('user.UserServiceImpl.queryLoginFail', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 0)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

// 新增订阅
export const addCastTalkUserAuth = ({dispatch}, params) => {
  return http('user.UserOpsServiceImpl.addUserVip', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('CAST_TALK_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

// 上传附件到七牛
const qiniuResource = Vue.resource(QINIU_API_ROOT)
export const qiniuUpload = ({dispatch}, params) => {
  return qiniuResource.save(params)
    .then((res) => {
      if (+res.data.code === 1000000) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

// 添加投研大咖说附件
export const addCastTalkAttactment = ({dispatch}, params) => {
  return http('user.UserServiceImpl.addCircleAttachment', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('CAST_TALK_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

// 查询用户所购买的投研大咖说
export const queryCastTalkUserItems = ({dispatch}, params) => {
  return http('user.UserOpsServiceImpl.queryUserItem', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

// 大咖说分类- 列表
export const getTypeList = ({dispatch}, params) => {
  return http('user.UserServiceImpl.circleTypeList', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 0)
        dispatch('SET_CLASSIFICATION_LIST', res.data)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}
// 大咖说分类- 新增
export const addCastTalkItem = ({dispatch}, params) => {
  return http('user.UserServiceImpl.insertCircleType', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 1)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

// 大咖说分类- 删除
export const deleteCastTalkItem = ({dispatch}, params) => {
  return http('user.UserServiceImpl.deleteCircleType', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 1)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

// 买方问答- 列表
export const getBuyerList = ({dispatch}, params) => {
  return http('user.UserServiceImpl.selectAllCircleBuyerQA', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 0)
        dispatch('SET_BUYER_LIST', res.data)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}
// 买方问答- 新增
export const addBuyerItem = ({dispatch}, params) => {
  return http('user.UserServiceImpl.insertCircleBuyerQA', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 1)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

//买方问答- 删除
export const deleteBuyerItem = ({dispatch}, params) => {
  return http('user.UserServiceImpl.deleteCircleBuyerQA', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 1)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

// 首页Banner- 列表
export const getHomeBannerList = ({dispatch}, params) => {
  return http('user.UserServiceImpl.selectAllBanner', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 0)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}
// 首页Banner- 新增
export const addHomeBannerItem = ({dispatch}, params) => {
  return http('user.UserServiceImpl.insertBanner', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 1)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

// 首页Banner- 删除
export const deleteHomeBannerItem = ({dispatch}, params) => {
  return http('user.UserServiceImpl.deleteBanner', params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CAST_TALK_NEED_FRESH', 1)
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}
